package com.ac.controller;

import org.bson.Document;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ac.aop.Auth;
import com.ac.exception.RRException;
import com.ac.model.LogSystem;
import com.ac.model.Page;
import com.ac.model.query.LogSystemQ;
import com.ac.response.HttpStatus;
import com.ac.response.Res;
import com.ac.service.AuthTokenService;
import com.ac.service.LogSystemService;
import com.ac.util.MapUtils;
import com.ac.util.StrUtils;

import lombok.AllArgsConstructor;

@RestController
@RequestMapping("log/system")
@AllArgsConstructor
public class LogSystemController {

	private final LogSystemService LogSystemService;
	private final AuthTokenService ts;

	@Auth
	@PostMapping(value = "list")
	public Res list(@RequestHeader("token") String token, @RequestBody Document map) {
		LogSystemQ uq = MapUtils.toBean(map, LogSystemQ.class);
		Page page = LogSystemService.list(uq);
		Res res = Res.success(null, page.getList(), page.getTotal());
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "save")
	public Res save(@RequestBody LogSystem api, @RequestHeader("token") String token) {
		if (StrUtils.isEmpty(api.getId()))
			LogSystemService.insert(api);
		else
			new RRException("操作失败", HttpStatus.BUG);
		Res res = Res.success("保存成功");
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "removeAll")
	public Res removeAll(@RequestBody LogSystem api, @RequestHeader("token") String token) {
		LogSystemService.removeAll(api);
		Res res = Res.success("删除成功");
		return ts.getToken(res, token);
	}

}
